{extend name="template/base" /}
{block name="content"}
<!--select2样式 start -->
<link href="__LIB__/select2-4.0.3/dist/css/select2.min.css" rel="stylesheet" />
<div class="page-container">
    <br><br>
    {include file="form" /}
    <div class="cl pd-5 bg-1 bk-gray">
        <span class="l">
            {tp:menu menu="add,delete,recyclebin" /}
            <a class="btn btn-primary radius mr-5" id="sou" href="{:url('/admin/hotel_time_price/get_excel',$searchCondition)}" >excel导出</a>
            <a class="btn btn-primary radius mr-5" id="xuan" style="display: none;"  href="javascript:;" onclick="xuan()">excel导出</a>
        </span>
        <span class="r pt-5 pr-5">
            共有数据 ：<strong>{$count ?? '0'}</strong> 条
        </span>
    </div>
    {php} use think\Db;{/php}
    <table class="table table-border table-bordered table-hover table-bg mt-20">
        <thead>
        <tr class="text-c">
            {include file="th" /}
            <th width="70">操作</th>
        </tr>
        </thead>
        <tbody>
        {volist name="list" id="vo"}
        <tr class="text-c">
            {include file="td" /}
            <td class="f-14">
                {if $vo.enable_status != 1}
                <a title="启用" onclick="disable('{$vo.id}','{$vo.enable_status}')" style="text-decoration:none" class="ml-5 label label-success radius">启用</a>
                {else /}
                <a title="禁用" onclick="disable('{$vo.id}','{$vo.enable_status}')" style="text-decoration:none;" class="ml-5 label label-warning radius">禁用</a>
                {/if}
                <a title="酒店房价" style="margin-top: 5px;" href="javascript:;" onclick="layer_open_success('酒店房价','/admin/hotel_room_price/index/id/{$vo.id}')" style="text-decoration:none" class="ml-5 label label-warning radius">酒店房价</a>
                {tp:menu menu='sedit' /}
                {tp:menu menu='sdelete' /}
            </td>
        </tr>
        {/volist}
        </tbody>
    </table>
    <div class="page-bootstrap">{$page ?? ''}</div>
</div>
{/block}

{block name='script'}
<script type="text/javascript" src="__LIB__/My97DatePicker/WdatePicker.js"></script>
<!--select2 js 插件-->
<script type="text/javascript" src="__LIB__/select2-4.0.3/dist/js/select2.min.js"></script>
<script>

    var room_type_id = $("#room_type_id").select2({
        language: "zh-CN", //设置 提示语言
        width: "100%", //设置下拉框的宽度
        theme: "classic"
    })
    {notempty name = "Request.param.room_type_id"}
    room_type_id.val("{$Request.param.room_type_id}").trigger("change");
    {/notempty}
</script>
<script>
    function layer_open_success(title, url, opt) {
        if (typeof opt === "undefined") opt = {nav: true};
        w = opt.w || "80vw";
        h = opt.h || "80vh";
        // 不支持vh,vw单位时采取js动态获取
        if (!attr_support('height', '10vh')) {
            w = w.replace(/([\d\.]+)(vh|vw)/, function (source, num, unit) {
                return $(window).width() * num / 100 + 'px';
            });
            h = h.replace(/([\d\.]+)(vh|vw)/, function (source, num, unit) {
                return $(window).height() * num / 100 + 'px';
            });
        }
        return layer.open({
            type: opt.type || 2,
            area: [w, h],
            fix: false, // 不固定
            maxmin: true,
            shade: 0.4,
            title: title,
            content: url,
            success: function (layero, index) {
                layero.find(".layui-layer-close").off("click").on("click", function () {
                    layer.close(index);
                    location.reload()
                });
                // 自动添加面包屑导航
                if (true === opt.nav) {
                    layer.getChildFrame('#nav-title', index).html($('#nav-title').html() + ' <span class="c-gray en">&gt;</span> ' + $('.layui-layer-title').html());
                }
                if (typeof opt.fn === "function") {
                    opt.fn(layero, index);
                }
            }
        });
    }

    //启用与禁用
    function disable(id,status){
        $.ajax({
            type: 'post',
            url: "{:url('admin/HotelTimePrice/enable')}",
            data: {id:id,status:status},
            dataType:'json',
            success:function(data){
                console.log(data);
                if (data.code == 1) {
                    layer.msg(data.msg, {
                        time: 1000, //1s后自动关闭
                    }, function () {
                        //关闭后刷新
                        location.reload();
                    });
                }
            }
        });
    }

    //全选
    $('#checkAlls').bind("click", function () {
        $("input[name='id[]']").prop("checked",this.checked);
        var sonChecked = $("input[name='id[]']").prop('checked');
        var allChecked = this.checked
        if (sonChecked && allChecked){
            $('#xuan').css('display','inline') // 显示选择excel导出数据
            $('#sou').css('display','none') // 隐藏部分或全部excel导出数据
        }else{
            $('#xuan').css('display','none')
            $('#sou').css('display','inline')
        }
        // console.log('你点击我了')
        // console.log('你是否被选中', this.checked)
        // console.log('下级的选框', $("input[name='id[]']").prop('checked'))
    });

    // 单选
    $("input[name='id[]']").bind('click', function () {
        var radioData =  $("input:checked[name='id[]']");
        // 选择数据的长度
        var len = radioData.serializeArray().length;
        if (len > 0) {
            $('#xuan').css('display','inline') // 显示选择excel导出数据
            $('#sou').css('display','none') // 隐藏部分或全部excel导出数据
        } else {
            $('#xuan').css('display','none')
            $('#sou').css('display','inline')
            $('#checkAlls').prop('checked',false); // 取消选择全选按钮
        }
        // console.log('你点击了单选')
        // console.log('点击的结果为：', this.checked)
        // console.log('你选择的数据是', radioData.serializeArray())
        // console.log('你选择的数据长度是',len)
    });

    function xuan() {
        var checkData = $("input:checked[name='id[]']");
        var dataArr = checkData.serializeArray();
        dataArr.push({name: 'search',value: 'all'})
        // console.log('选择的数据是', checkData.length)
        // console.log('选择的数据是', checkData.serializeArray())
        // console.log('选择的数组是', dataArr)
        $.ajax({
            type: 'post',
            url: '/admin/hotel_time_price/get_excel',
            data: dataArr,
            success: function (res) {
                if (res.code == 5){
                    // console.log('data', res);
                    location.href = res.msg; // 下载文件
                    // 删除文件
                    unclinkFile(res.msg)
                    layer.msg('导出数据成功');
                } else {
                    layer_open('excel导出数据','/admin/pub/role/id/1')
                }
            }
        });
    }
    var  value = 1;
    setTimeout(unclinkFile,1200000,value); // 延迟20分钟1200000
    // 15分钟后删除文件-900000
    function unclinkFile(val) {
        console.log(val)
        setTimeout(function () {
            // 删除文件
            $.ajax({
                type: 'post',
                url: '/admin/order/del',
                data: {val:val},
                success: function (data) {
                    console.log('data', data)
                    if (data.code != 5) {
                        console.log('删除失败', data.msg)
                    }
                }
            })
        },120000,val); // 2分钟后执行
    }

</script>
{/block}

